<template>
  <div class="editor-adress-con">
    <div class="navbar">
      <div class="back" @click="goBack"></div>
      <div class="nav-title">添加收货地址</div>
    </div>
    <div class="name detail" @click="editName">
      <div>收货人</div>
      <div style="color: gray">{{ name }}</div>
    </div>
    <hr />
    <div class="birthday detail" @click="editTel">
      <div>手机号码</div>
      <div style="color: gray">{{ tel }}</div>
    </div>
    <hr />
    <div class="city detail" @click="chooseArea">
      <div>选择地区</div>
      <div style="color: gray">
        <div v-for="(item, index) in area" :key="index" class="area">
          {{ item }}
        </div>
      </div>
      <div class="right-arrow">
        <img src="../../assets/img/order/right-arrow.png" alt="" width="11px" />
      </div>
    </div>
    <hr />
    <div class="detail-location detail" @click="editDetail">
      <div>详细地址</div>
      <div class="chooseArea">{{ detailLocation }}</div>
    </div>
    <div class="save-btn" @click="add">保存</div>
    <picker
      :result="regionPopup"
      @changepop="changePop"
      @getRegion="getChooseRegion"
    />
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import picker from "../sub-components/AddressSpicker.vue";
export default {
  data() {
    return {
      username: "",
      name: "",
      tel: "",
      area: [],
      detailLocation: "",
      regionPopup: false,
    };
  },
  components: {
    picker,
  },
  created() {
    if (localStorage.getItem("UserInfo")) {
      this.username = window.JSON.parse(
        localStorage.getItem("UserInfo")
      ).username;
    }
  },
  methods: {
    goBack() {
      this.$router.push("/idssp/order/myAdress");
    },
    // 编辑昵称
    editName() {
      this.$messagebox({
        $type: "prompt",
        title: "请输入昵称",
        message: " ",
        showInput: true,
        showCancelButton: true,
        confirmButtonText: "提交",
        cancelButtonText: "取消",
      })
        .then(({ value, action }) => {
          if (action == "confirm") {
            this.name = value;
          }
        })
        .catch((err) => {
          // console.log(err);
        });
    },
    editTel() {
      this.$messagebox({
        $type: "prompt",
        title: "请输入电话",
        message: " ",
        showInput: true,
        showCancelButton: true,
        confirmButtonText: "提交",
        cancelButtonText: "取消",
      })
        .then(({ value, action }) => {
          if (action == "confirm") {
            this.tel = value;
          }
        })
        .catch((err) => {
          // console.log(err);
        });
    },
    editDetail() {
      this.$messagebox({
        $type: "prompt",
        title: "请输入详细地址",
        message: " ",
        showInput: true,
        showCancelButton: true,
        confirmButtonText: "提交",
        cancelButtonText: "取消",
      })
        .then(({ value, action }) => {
          if (action == "confirm") {
            this.detailLocation = value;
          }
        })
        .catch((err) => {
          // console.log(err);
        });
    },
    add() {
      // 拼接地址
      let location = "";
      this.area.forEach((item) => {
        location += item;
      });
      location += this.detailLocation;
      // 请求后端
      let params = {
        username: this.username,
        name: this.name,
        tel: this.tel,
        location: location,
      };
      this.$http.post("/addAdress", params).then((res) => {
        if (res.data.errcode === 200) {
          Toast("添加成功!!!");
          this.$router.push("/idssp/order/myAdress");
        } else {
          Toast("添加失败!!!");
        }
      });
    },
    chooseArea(){
      this.regionPopup = true;
      // 
      this.area = ['北京市 ','市辖区 ','东城区 ']
    },
    changePop(val) {
      this.regionPopup = val;
    },
    getChooseRegion(val) {
      this.area = val;
    },
  },
};
</script>
<style lang='scss' scoped>
.editor-adress-con {
  font-size: 15px;
  padding-top: 50px; //如果不写则页面会显示不完全
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    width: 100%;
    height: 44px;
    background-color: #e25b45;
    text-align: center;
    color: white;
    font-size: 16px;
    align-items: center;
    .back {
      width: 50px;
      height: 100%;
      position: absolute;
      left: 20px;
      background: url("../../assets/img/back.png") no-repeat;
      background-size: 40% 40%;
      background-position-y: center;
    }
    .nav-title {
      margin: 0 auto;
    }
  }
  .detail {
    padding: 14px 20px 10px 20px;
    color: #1e1e1e;
    line-height: 15px;
    display: flex;
    justify-content: space-between;
    .area {
      line-height: 20px;
      width: 220px;
    }
    .right-arrow {
      display: flex;
      align-items: center;
    }
    .chooseArea {
      width: 255px;
      color: gray;
    }
  }
  .save-btn {
    margin: 30px 7px;
    background-color: #fd5e01;
    color: #fff;
    width: 96%;
    height: 34px;
    border-radius: 50px;
    text-align: center;
    line-height: 34px;
    font-size: 14px;
  }
}
</style>